package common.util;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import common.customer.bean.Customer;
import common.system.bean.City;
/**
 * struts 帮助类
 * @author Administrator
 *
 */
public class StrutsUtil {
	private static Map<String,SimpleDateFormat> formats = new HashMap<String,SimpleDateFormat>();
	/**
	 * 通过session获得当前城市
	 * @param session
	 * @return
	 */
	public static int getCurrentCityBySession(HttpSession session){
		Object city = session.getAttribute("city");
		if(city!=null){
			return ((City)city).getId();
		}else{
			Object user = session.getAttribute("user");
			if(user!=null){
				return ((Customer)user).getCity().getId();
			}else{
				return 1;
			}
		}
	}
	/**
	 * 验证用户是否登录
	 * @param session
	 * @return
	 */
	public static boolean validateLogin(HttpSession session){
		Object city = session.getAttribute("user");
		if(city!=null){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 
	 */
	public static SimpleDateFormat getFormat(String format){
		if(format==null || "".equals(format.trim())){
			return null;
		}else{
			SimpleDateFormat s = formats.get(format);
			if(s!=null){
				return s;
			}else{
				s = new SimpleDateFormat(format);
				formats.put(format, s);
				return s;
			}
		}
	}
	/**
	 * 验证验证码是否正确
	 * @param session
	 * @param validationCode
	 * @return
	 */
	public static boolean validationCodeIsRight(HttpSession session ,String validationCode){
		if (validationCode == null || "".equals(validationCode.trim())) {
			return false;
		}
		String validationCodeSess = session.getAttribute("validationCode") == null ? null
				: session.getAttribute("validationCode").toString().trim();
		if (!validationCode.equals(validationCodeSess)) {
			return false;
		}
		return true;
	}
}
